#ifndef _HLFS_MISC_H_
#define _HLFS_MISC_H_

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdlib.h>
//#include "hlfs.h"
#include "address.h"
#include "storage.h"
#include "bs_local.h"

static uint64_t get_current_time(void)
{
	struct timeval t;

	if (gettimeofday(&t, NULL)) {
		g_message("Get current time error!\n");
		return -1;
	}
	return (uint64_t) t.tv_sec * 1000 + t.tv_usec / 1000;	
}

static char *build_segfile_name(uint64_t segno)
{
    	char s[64];
    	sprintf(s, "%llu", segno);
    	gchar * segfile_name = g_strconcat(s, ".", "seg", NULL);
    	return segfile_name;
}
static uint32_t get_segfile_no(const char * segfile){
        const gchar *basename = g_basename(segfile);
        gchar **v = g_strsplit(basename,".",2);
        uint32_t segno = atol(v[0]);
        g_strfreev(v);
        return segno;
}

static char *build_segfile_name_by_address(uint64_t storage_address)
{
	uint32_t segno  = get_segno(storage_address);
    	char s[64];
    	sprintf(s, "%d", segno);
    	gchar * segfile_name = g_strconcat(s,".","seg",NULL);
    	return segfile_name;
}

static char *read_block(struct back_storage *storage ,uint64_t storage_address,uint32_t block_size)
{
    int ret = 0;
    uint32_t offset = get_offset(storage_address); 
    gchar * segfile_name = build_segfile_name(get_segno(storage_address));

    bs_file_t file = storage->bs_file_open(storage,segfile_name,BS_READONLY); 
    if(file==NULL){
        g_message("can not open segment file %s\n",segfile_name);
        g_free(segfile_name);
        return NULL;
    }

    char * block = (char*)g_malloc0(block_size);
    if (NULL == block) {
	    printf("%s - Allocate Error!\n",__func__);
	    block = NULL;
	    goto out;
    }
    if(block_size != storage->bs_file_pread(storage,file,block,block_size,offset)){
        g_free(block);
        block = NULL;
        goto out;
    }

out:
    storage->bs_file_close(storage,file);
    g_free(segfile_name);    	
    return block;
}

/* 
 * uri format --
 * local:///tmp/testenv
 * hdfs:///tmp/testenv
 * hdfs://localhost:8020/tmp/testenv
 * hdfs://localhost/tmp/testenv
 * hdfs://192.168.0.1:8020/tmp/testenv
 * */
static int parse_from_uri(const char *uri,char ** head, char** hostname ,char** dir,int* port){
    gchar **v=NULL;
    gchar **v1=NULL;
    gchar **v2=NULL;
    v = g_strsplit(uri,"://",2);
    if(g_strv_length(v)!=2){
       g_strfreev(v);
       return -1; 
    }
    *head = g_strdup(v[0]);

    if( v[1][0] == '/' ){
        g_message("default localhost for hostname");
        *hostname = g_strdup("default");
        *dir = g_strdup(v[1]);
        *port = 0; 
    }else{
        v1 = g_strsplit(v[1],"/",2);
        if(g_strv_length(v1) < 2){
           g_strfreev(v);
           g_strfreev(v1);
           return -1;
        }
        v2 = g_strsplit(v1[0],":",2);
        if(g_strv_length(v2)!=2){
           *hostname = g_strdup(v1[0]);
           *dir = g_strdup(v1[1]);
           *port = 8020;
        }else{
           *hostname = g_strdup(v2[0]);
           *dir = g_strdup(v1[1]);
           *port = atoi(v2[1]);
        }
    }
    g_strfreev(v);
    g_strfreev(v1);
    g_strfreev(v2);
    return 0;
}

#endif 
